package redis.inaction.xx.utils;

import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import static redis.inaction.xx.utils.ResponseCode.SUCC;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResponse {

    private static final String COMMON_SUCC_MSG = "success";

    private boolean succ;
    private String code;
    private String msg;
    private Object data;

    public static String successResponse(){
        return new Gson().toJson(createSuccess(COMMON_SUCC_MSG, null));
    }

    public static String successResponse(Object data){
        return new Gson().toJson(createSuccess(COMMON_SUCC_MSG, data));
    }

    public static CommonResponse createFail(String code, String msg){
        return create(false, code, msg, null);
    }

    public static CommonResponse createFail(String code, String msg, Object data){
        return create(false, code, msg, data);
    }

    public static CommonResponse createSuccess(String msg, Object data){
        return create(true, SUCC, msg, data);
    }

    private static CommonResponse create(boolean succ, String code, String msg, Object data){
        return new CommonResponse(succ, code, msg,data);
    }
}
